Import-CSV... | ForEach fails because some customAttribute fields are empty (value does not apply to that contact)
When I attempt to import from a CSV file, the operation fails because many of the customAttribute fields are empty (if the contact does not have that particular attribute, the field is empty, but it could be filled with a value for another contact).Import-CSV "C:\Import.csv" | ForEach{ New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail-customAttribute1 $_.customAttribute1-customAttribute2 $_.customAttribute2-customAttribute3 $_.customAttribute3-customAttribute4 $_.customAttribute4-customAttribute5 $_.customAttribute5-customAttribute6 $_.customAttribute6-customAttribute7 $_.customAttribute7-customAttribute8 $_.customAttribute8-customAttribute9 $_.customAttribute9-customAttribute10 $_.customAttribute10-customAttribute11 $_.customAttribute11-customAttribute12 $_.customAttribute12-customAttribute13 $_.customAttribute13-customAttribute14 $_.customAttribute14-OrganizationalUnit "contoso.com/Staff"}How do you resolve this?What if Contact 1 does not have a value in customAttribute1?How can I tell Exchange/Powershell:"Skip the customAttribute fields where no value is present and continue to populate those fields that DO have a value."
February 15th, 2010 12:10am

Is it the import of the csv that's failing, or the creation of the mailcontact? Can you post the error message you're gettig?
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2010 12:29am

If the csv is importing correctly, then you're going to need to test each of the custom attributes on the incoming object and only use the parameters for the ones that actually have something in them.I don't have an Exchange server handy to test it on, but I think this approach should work.Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" 'foreach ($attnum in 1..14) {if (iex ('$_.customAttribute.' + $attnum)){$makenew += (' -customAttribute' + $attnum ' + '$_.customAttribute' + $attnum)}iex $makenew}
February 15th, 2010 1:45am

Is it the import of the csv that's failing, or the creation of the mailcontact? Import fails because the cmdlet I entered does not seem to know what to do do with empty custom attribute fields. Can you post the error message you're gettig?Not right now. But I can later on.
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2010 5:38pm

OK. I've been out and finally had a chance to look at this again.Here is the error message: New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At C:\Updates\ImportContacts.ps1:1 char:178 That's because there is no value for the parameter in question (for that contact). Now, when I run yours, the following message displays (in the usual red letters): [PS] C:\Updates>C:\Updates\ImportContacts1.ps1 Unexpected token ' + ' in expression or statement. At C:\Updates\ImportContacts1.ps1:7 char:49 + $makenew += (' -customAttribute' + $attnum ' + '$ <<<< _.customAttribute' + $attnum) So what should be changed?
February 22nd, 2010 3:54pm

My fault. I had an extraneous ' in the script. Try replacing that line with this one:$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2010 4:20pm

Thank you, I will try that later today.Otherwise, am I missing something or are there more of these { than these } ? (I believe they're called curly brackets) I bolded the first and the last, and then bolded and underlined the next two that open. Does there have to be one } for each { ?####################################Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" 'foreach ($attnum in 1..14) {if (iex ('$_.customAttribute.' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)}iex $makenew} ################################## Lastly, I can understand the logic of just about everything (not that I could have come up with this myself) except this part:if (iex ('$_.customAttribute.' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)Are you telling PS:For each attribute in columns 1 to 14 of the CSV file, if the Custom attribute has an attribute ('$_.customAttribute.' + $attnum), execute the $makenew variable (as defined earlier in the script). And then what does the following mean: $makenew += ... etc.BTW, should there be a period after $_.customAttribute. <- in the sentence above?
February 22nd, 2010 10:49pm

Good catch. You are correct, the brackets are out of balance. There needs to be another closing bracket at the end, and that period should not be there.iex (invoke-expression) takes a string, and attempts to execute it as a powershell command.We start off with this:$makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" 'which creates a string of the command, along with the parameters we know will always be included. Note the single quotes - none of the powershell variables will be expanded at this point.Then through a counter of 1..14 we check whether the current pipeline object has a value for the customattribute for the current counter number. If it does, we take the counter number and construct another string of '-customattribute' plus the counter number, a space and the string '$_.customattribute' plus the counter number again. Then the += concatenates that string onto our original $makenew string.i.e. if the current counter number is 4, and we have a value in $_.custom attribute4, it will counstruct the string " -CustomeAttribute4 $_.CustomAttribute4" and append it to the rest of the command already in $makenew.Bascially we're just building the command string that you would type in to set just the custom attributes you have values for. I'm using an iex early in process to invoke the test of $_.customattributex, so that we can use the loop counter x to fill out the specific custom attribute we're testing for.When we've gone through all 14 possible custom attributes, we invoke the string we've built.Mostly is just an exercise in string manipulation, to get the string representation of a powershell command we'll execute using iex after we get it all built.
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2010 11:17pm

No results.Tried it with the brackets this way:########################################Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" ' foreach ($attnum in 1..14) {if (iex ('$_.customAttribute' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)}} iex $makenew } ########################################And this way:########################################Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" ' foreach ($attnum in 1..14) {if (iex ('$_.customAttribute' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)} iex $makenew }}######################################## In both cases I am prompted for additional information:cmdlet foreach at command pipeline position 2Supply values for the following parameters:Process[0]: James, Do you think I would get more input in the PowerShell forum? I need to make this work soon and am just encountering one obstacle after another.
February 23rd, 2010 4:19pm

The first one should be right. It shouldn't be that hard to debug. Do this, and insead of trying to invoke $makenew, it will simply write it out to the console. That should give you some evidence of where it's breaking down. Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" ' foreach ($attnum in 1..14) {if (iex ('$_.customAttribute' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)}}#iex $makenew$makenew} ########################################
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2010 4:29pm

This is exactly what I have in the script, except the domain name which is changed to Contoso...It does not work.##########################################Import-CSV "F:\ParentImport.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Parents" ' foreach ($attnum in 1..14) {if (iex ('$_.customAttribute' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)}} #iex $makenew $makenew } ##############################################This is what you see on the screen, in the EMS, after executing the script:[PS] F:\Scripts>F:\Scripts\ImportCSV.ps1 cmdlet foreach at command pipeline position 2Supply values for the following parameters:Process[0]:
February 23rd, 2010 10:49pm

It looks like it's choking on the first record hitting the first foreach loop.Are you sure that csv is good?
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2010 11:11pm

Try this:Import-CSV "F:\ParentImport.csv" | ForEach { $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Parents" ' foreach ($attnum in 1..14) {if (iex ('$_.customAttribute' + $attnum)){$makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum)}} #iex $makenew $makenew }
February 23rd, 2010 11:13pm

Are you sure that csv is good?I think so:When I first tried it, I just did thisNew-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Parents"Candidly thinking it would just somehow "grab" the customAttributes.It DID import the names of the parents. Without the custom attributes though...That's when I modified the script as you see in my first post.Here are the first two lines of the CSV file:sn,givenName,displayName,mail,customAttribute1,customAttribute2,customAttribute3,customAttribute4,customAttribute5,customAttribute6,customAttribute7,customAttribute8,customAttribute9,customAttribute10,customAttribute11,customAttribute12,customAttribute13,customAttribute14Smith,David,David Smith,davesmith@yahoo.com,,2011,,,,,,,,,,,,BTW, I thought I'd ask about this in the Scripting Guy forum - where I see you post as well. I do appreciate your help and don't think I'm giving up on you. It's just that I promised to do this, thinking that it I would not encounter a problem like this. So people are starting to ask me when this will get done.
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2010 11:28pm

I understand. I have probably contributed to the frustration factor by not testing the code I posted, but that's not always practical.Honestly, I believe the syntax error of having that first opening brace on the wrong line will fix the error.
February 23rd, 2010 11:39pm

OK, this time something happened... except the MailContacts were never actually created.Get-MailContact?Nothing.Get-Contact?Nothing.OK, but what happened?The script executed and this is what Start-Transcript recorded (summarized):[PS] F:\Scripts>F:\Scripts\ImportCSV1.ps1New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute2$_.customAttribute2New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute2$_.customAttribute2New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute1$_.customAttribute1 -customAttribute4$_.customAttribute4New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute4$_.customAttribute4New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute3$_.customAttribute3New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" -customAttribute3$_.customAttribute3[...] Etc, etc.Maybe that # in front of #iex $makenew needs to be removed, and this as well?$makenew
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2010 11:51pm

Hi, Yes u need to uncomment it #iex $makenew to make it iex $makenew and remove $makenew or just comment it like #$makenew Regards,Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
February 24th, 2010 12:03am

OK, will give this a try tonight.
Free Windows Admin Tool Kit Click here and download it now
February 24th, 2010 12:09am

Yes, that's expected with the iex $makenew commented out. If you uncomment that line, it will begin to execute the commands that are being written to the console.
February 24th, 2010 12:38am

It didn't work. It just didn't work. This is what I run: ########################################### Import-CSV "C:\Updates\ParentImport.csv" | ForEach { $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" ' foreach ($attnum in 1..14) { if (iex ('$_.customAttribute' + $attnum)){ $makenew += (' -customAttribute' + $attnum + '$_.customAttribute' + $attnum) } } iex $makenew #$makenew } Result: New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1$_'. At line:1 char:167 + New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -Organizational Unit "acme.loc/Parents" -customAttribute1$_. <<<< customAttribute1 And so on... ####################################### I thought it would be clever to enter a 0 as the customAttribute value for every column that did not have one already, and execute my original script. That way it could not complain about there being no value. This is what I mean: sn,givenName,displayName,mail,customAttribute1,customAttribute2,customAttribute3,customAttribute4,customAttribute5,customAttribute6,customAttribute7,customAttribute8,customAttribute9,customAttribute10,customAttribute11,customAttribute12,customAttribute13,customAttribute14 Smith,David,David Smith,davesmith@aol.com,0,2011,0,0,0,0,0,0,0,0,0,0,0, No luck: New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1$_'. At line:1 char:167 + New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -Organizational Unit "acme.loc/Parents" -customAttribute1$_. <<<< customAttribute1 -customAttribute2$_.customAttribute2 -customAttribute3$_ .customAttribute3 -customAttribute4$_.customAttribute4 -customAttribute5$_.customAttribute5 -customAttribute6$_.customAttrib ute6 -customAttribute7$_.customAttribute7 -customAttribute8$_.customAttribute8 -customAttribute9$_.customAttribute9 -customA ttribute10$_.customAttribute10 -customAttribute11$_.customAttribute11 -customAttribute12$_.customAttribute12 -customAttribut e13$_.customAttribute13 [snip] Does 0 not count as a value? ####################################### Note: the import DOES work if I take out all the customAttribute stuff ...
Free Windows Admin Tool Kit Click here and download it now
February 24th, 2010 6:03pm

0 does count as a value. If you look at the console output, you'll see the space between the parameter and the parameter value is missing. We need to get a space in there. Import-CSV "C:\Updates\ParentImport.csv" | ForEach { $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" ' foreach ($attnum in 1..14) { if (iex ('$_.customAttribute' + $attnum)){ $makenew += (' -customAttribute' + $attnum + ' ' + '$_.customAttribute' + $attnum) } } iex $makenew #$makenew }
February 24th, 2010 6:08pm

I just realized part of the problem (I think). 0 counts as a value, but it may evaluate as $false on a straight test if Powershell's type conversion reads it as a int instead of as a string. I wasn't expecting 0's as vaules when I wrote that test, but I didn't know what the data would look like.Try this: Import-CSV "C:\Updates\ParentImport.csv" | ForEach { $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" ' foreach ($attnum in 1..14) { if (iex ('$_.customAttribute' + $attnum + ' -ne $null')){ $makenew += (' -customAttribute' + $attnum + ' ' + '$_.customAttribute' + $attnum) } } iex $makenew #$makenew }
Free Windows Admin Tool Kit Click here and download it now
February 24th, 2010 6:14pm

-ne $null Yes, I was trying to figure out how to do that!Was trying -eq 201* (year of graduating class is the custom attribute in fact) but that did not work.Anyway, I'll give this a try - and hopefully have good news by tomorrow.
February 24th, 2010 9:32pm

It doesn't work. ########################################################### First, just so you can all see the CSV target (in case something is wrong there), here are the headings and the values for the first person: sn,givenName,displayName,mail,customAttribute1,customAttribute2,customAttribute3,customAttribute4,customAttribute5,customAttribute6,customAttribute7,customAttribute8,customAttribute9,customAttribute10,customAttribute11,customAttribute12,customAttribute13,customAttribute14 Smith,David,David Smith,davesmith@aol.com,0,2011,0,0,0,0,0,0,0,0,0,0,0,0 ########################################################## 1st script you suggested New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:165 + New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -Organizational Unit "acme.loc/Parents" -customAttribute1 <<<< $_.customAttribute1 2nd script you suggested (with $null) New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:165 + New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -Organizational Unit "acme.loc/Parents" -customAttribute1 <<<< $_.customAttribute1 -customAttribute2 $_.customAttribute2 -customAttribute3 $_.customAttribute3 -customAttribute4 $_.customAttribute4 -customAttribute5 $_.customAttribute5 -customAttribute6 $_.custom Attribute6 -customAttribute7 $_.customAttribute7 -customAttribute8 $_.customAttribute8 -customAttribute9 $_.customAttribute9 -customAttribute10 $_.customAttribute10 -customAttribute11 $_.customAttribute11 -customAttribute12 $_.customAttribute12 -cu stomAttribute13 $_.customAttribute13 -customAttribute14 $_.customAttribute14 2nd script targeting CSV file with 1 instead of no value or 0 for the value. I also put letters as values ("val" for example). New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:165 + New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -Organizational Unit "acme.loc/Parents" -customAttribute1 <<<< $_.customAttribute1 -customAttribute2 $_.customAttribute2 -customAttribute3 $_.customAttribute3 -customAttribute4 $_.customAttribute4 -customAttribute5 $_.customAttribute5 -customAttribute6 $_.custom Attribute6 -customAttribute7 $_.customAttribute7 -customAttribute8 $_.customAttribute8 -customAttribute9 $_.customAttribute9 -customAttribute10 $_.customAttribute10 -customAttribute11 $_.customAttribute11 -customAttribute12 $_.customAttribute12 -cu stomAttribute13 $_.customAttribute13 -customAttribute14 $_.customAttribute14 It's not the script: this doesn't work at the command line either: [PS] C:\>New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1 2050 -OrganizationalUnit "acme.loc/Parents" New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:124 + New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1 <<<< 2050 -OrganizationalUnit "acme.loc/Parents" [PS] C:\>New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1 "2050" -OrganizationalUnit "acme.loc/Parents" New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:124 + New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1 <<<< "2050" -OrganizationalUnit "acme.loc/Parents" [PS] C:\>New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1="2050" -OrganizationalUnit "acme.loc/Parents" New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1="2050"'…. [PS] C:\>New-MailContact -Name "David Smith" -FirstName David -LastName Smith -ExternalEmailAddress daveSmith@aol.com -customAttribute1=2050 -OrganizationalUnit "acme.loc/Parents" New-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1=2050'. At line:1 char:129… Let's try this: [PS] C:\>Set-MailContact hbf@orange.fr customAttribute1 2050 Set-MailContact : A parameter cannot be found that matches parameter name 'customAttribute1'. At line:1 char:16 + Set-MailContact <<<< hbf@orange.fr customAttribute1 2050 [PS] C:\>Set-MailContact hbf@orange.fr -customAttribute1 2050 [PS] C:\>Get-MailContact hbf@orange.fr | fl custom* CustomAttribute1 : 2050 CustomAttribute10 : CustomAttribute11 : CustomAttribute12 : CustomAttribute13 : CustomAttribute14 : So I add the dash before "customAttribute*" in the CSV file. Now it runs... but still does not import the value! It should either import the string "val" or an integer like 2015. When I look at the newly created MailContacts in the EMC, there are no values for anyone. None whatsoever. ############################################################################## OK, this runs w/o a problem except it does NOT create the custom attributes: ###### Import-CSV "C:\Updates\ParentImport.csv" | ForEach { $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" ' foreach ($attnum in 1..14) { if (iex ('$_.customAttribute' + $attnum)){ $makenew += (' -customAttribute' + $attnum + ' ' + '$_.customAttribute' + $attnum) } } iex $makenew #$makenew } ############################################################################## So why can't it import either a string or an integer?
Free Windows Admin Tool Kit Click here and download it now
February 25th, 2010 4:15pm

I don't know. Let's try a different tack. Import-CSV "C:\Updates\ParentImport.csv" | ForEach { New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "acme.loc/Parents" $makenew = 'Set-MailContact $_.displayName ' foreach ($attnum in 1..14) { if (iex ('$_.customAttribute' + $attnum)){ $makenew += (' -customAttribute' + $attnum + ' ' + '$_.customAttribute' + $attnum) } } iex $makenew #$makenew }
February 25th, 2010 4:35pm

Hi, Yes the error is correct. You cannot pass CustomAttributes (1--15) to New-MailContact command. Have a look on: http://technet.microsoft.com/en-us/library/bb124519(EXCHG.80).aspx Also You should divide your script into two sections 1- Create Mail-Contact 2- Update Mail-Contact Also in 2nd section use some unique value instead of Display Name in $makenew = 'Set-MailContact $_.displayName ' otherwise there is chance that multiple users have similar display names and wrong attributes may apply on wrong contacts. If u are sure that DisplayNames are unique then u can carry on like that. Regards, Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
Free Windows Admin Tool Kit Click here and download it now
February 25th, 2010 4:43pm

OK this is not going to work.I don't think Set-MailContact knows "how" to retrieve values from a CSV file and even if it does, it's too late.Besides copying and pasting, how can I place -Set-MailContactbefore the Alias of each person in a listJohnSmithJaneSmithAlfredJonesSallyRichardsetc.and then...customAttributeafter...We will then add the custom Attribute number and value as appropriate...
February 26th, 2010 4:39pm

You are correct. Set-MailContact does not know how to read a csv. That is why you have to write script.If your names are in a file (list.txt) this should do what you have asked:$sets = @()gc "c:\somedir\list.txt" |%{$sets += ('set-mailcontact ' + $_ + ' -customAttribute')}$sets | out-file "c:\somedir\set_cmds.txt"
Free Windows Admin Tool Kit Click here and download it now
February 26th, 2010 5:15pm

OK, this combination finally worked:Import-CSV "C:\Import.csv" | ForEach{ $makenew = 'New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/Staff" '}And (with my information):$sets = @()gc "c:\somedir\list.txt" |%{$sets += ('set-mailcontact ' + $_ + ' -customAttribute')}$sets | out-file "c:\somedir\set_cmds.txt"Note:I needed the list to be alphabetical by last name which was attained this way:Get-Contact | Sort LastName | Get-MailContact | fl alias | out-file C:\list.txtCustom Attributes needed to be entered manually once the list was conifgured with the $sets = @() (etc.) command.
March 2nd, 2010 4:36pm

Is there a good resource, book or online source, that would demonstrate, with simple examples, how to construct script elements like these:$sets = @()What's that?%{$sets += ('set-mailcontact ' + $_ + ' -customAttribute')} Foreach - whatever $sets is...I understand what it did, since I can see the outcome, but I don't see how to construct this.+= is somehow taking elements from the list and putting set-mailcontact before and -customAttribute after.Does $_ represent whatvever came from the list?
Free Windows Admin Tool Kit Click here and download it now
March 2nd, 2010 4:49pm

The Getting Started Guide from Powershell V1 is a good basic primer It's part of the Documentation Pack that can be downloaded from here:http://www.microsoft.com/downloads/details.aspx?familyid=B4720B00-9A66-430F-BD56-EC48BFCA154F&displaylang=en$sets = @()This is initializing an empty array. We're adding strings to $sets. The default behaviour of Powershell when adding strings is to concatenate them together. In order to tell it we really want them added to an array, we need to explicitly make it an array before we start adding the strings to it.+= is a kind of shortcut operator.$a += $b is that same as $a = $a + $bWhen you do get-content on a text file, the result is an array. Each line is an element of the array. When you send an array down the pipeline using foreach-object (%) it sends each element of the array, and the subsequent operations are performed on each one. The current object (in this case a line from the text file) in the pipeline is referenced as $_.%{$sets += ('set-mailcontact ' + $_ + ' -customAttribute')Basically says, for each line in that text file, take the literal string 'set-mailcontact ', append that line to it, and the append another literal string ' -customAttribute' to the end of that. The parens group that result together and then it's added to the $sets array. When it's gone through all of the elements of the arral (lines of the text file), $sets will be an array of the strings that were built around the lines from the text file.
March 2nd, 2010 5:27pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics